<template>
  <div class="mod-role">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-input
          v-model="dataForm.name"
          placeholder="角色名称"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button v-if="isAuth('sysRole:query')" @click="getDataList()"
          >查询</el-button
        >
        <el-button
          v-if="isAuth('sysRole:add')"
          type="primary"
          @click="AddHandle()"
          >新增</el-button
        >
        <el-button
          v-if="isAuth('sysRole:delete')"
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
          >批量删除</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="id"
        header-align="center"
        align="center"
        width="80"
        label="ID"
      >
      </el-table-column> -->
      <el-table-column
        prop="roleName"
        header-align="center"
        align="center"
        label="角色名称"
      >
      </el-table-column>
      <el-table-column
        prop="code"
        header-align="center"
        align="center"
        label="角色编码"
      >
      </el-table-column>
      <el-table-column
        prop="remark"
        header-align="center"
        align="center"
        label="备注"
      >
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="状态"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 2" size="small" type="danger"
            >停用</el-tag
          >
          <el-tag v-else-if="scope.row.status === 3" size="small" type="danger"
            >删除</el-tag
          >
          <el-tag v-else size="small">正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="createDateTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
        v-if="
          isAuth('sysRole:edit') ||
            isAuth('sysRole:delete') ||
            isAuth('sysRole:roleMenu')
        "
      >
        <!-- v-if="isAuth('sys:role:update')" -->
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('sysRole:edit')"
            type="text"
            size="small"
            @click="UpdateHandle(scope.row)"
            >修改</el-button
          >
          <!-- v-if="isAuth('sys:role:delete')" -->
          <el-button
            v-if="isAuth('sysRole:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="isAuth('sysRole:roleMenu')"
            type="text"
            size="small"
            @click="RoleToMenu(scope.row)"
            >角色授权</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[5, 10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
    <!-- 授权菜单 -->
    <authorization
      v-if="authorizationVisible"
      ref="authorization"
    ></authorization>
  </div>
</template>

<script>
import AddOrUpdate from "./role-add-or-update";
import Authorization from "./role-authorization.vue";
import { GetRoleListApi, DeleteRoleApi } from "@/api/role";
export default {
  data() {
    return {
      dataForm: {
        name: ""
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      authorizationVisible: false
    };
  },
  components: {
    AddOrUpdate,
    Authorization
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      GetRoleListApi(this.dataForm)
        .then(res => {
          this.dataList = res.data.data.items;
          this.totalPage = res.data.data.totalCount;
          this.dataListLoading = false;
        })
        .catch(err => {
          window.console.log(err);
          this.dataListLoading = false;
        });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增
    AddHandle() {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(null, true);
      });
    },
    // 修改
    UpdateHandle(item) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(item, false);
      });
    },
    // 删除
    deleteHandle(item) {
      var ids = item
        ? [item.id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      window.console.log(ids);

      var names = item
        ? [item.roleName]
        : this.dataListSelections.map(item => {
            return item.roleName;
          });
      this.$confirm(
        `确定对[${names.join(",")}]进行[${item ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          DeleteRoleApi(ids)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataList();
                  }
                });
              } else {
                this.$message.error(res.data.message);
              }
            })
            .catch(err => {
              window.console.log(err);
              this.$message.error(err.message);
            });
        })
        .catch(() => {});
    },
    // 角色授权菜单
    RoleToMenu(row) {
      this.authorizationVisible = true;
      this.$nextTick(() => {
        this.$refs.authorization.init(row.id);
      });
    }
  }
};
</script>
